/**
 * 分类管理JS
 * @author Assassin
 */
//加载treeTable数据
	function selectTreeTableData() {
		shiro.ajax({
 			url: '/goodsType/treeTable',
 			data:{},
 			success: function(data) {
 				if(data.result == 0) {
 					var treeTableData = '';
 					$.each(data.data, function (index, val) {
						if(val.typeLevel == 1) {
							treeTableData += 
		 	                      '<tr data-tt-id="' + val.typeId + '">'
		 	                    + ' <td style="text-align: center;"><input type="checkbox" value="' +  val.typeId + '" name="typeIdCheckBox"  onclick="javascript:dynamicCheck(this);"/></td>'
								+ '	<td>' + shiro.util.formatTableData("-",val.typeLevel) + '</td>'
								+ ' <td>' + shiro.util.formatTableData("-",val.typeName)+ '</td>'
								+ ' <td>' + shiro.util.formatTableData("-",val.typeDesc) + '</td>'
		 						+ ' <td style="text-align: center;">'
		 						+ '		<a  class="link-add" title="添加" href="javascript:;" onclick="javascript:addSubGoodsType(' + val.typeId + ');">'
		 						+ '			添加'
		 						+ '		</a>'
		 						+ '		<a   class="link-update" title="修改" href="javascript:;" onclick="javascript:editGoodsType(' + val.typeId + ');">'
		 						+ '			修改'
		 						+ '		</a>'
		 						+ '		<a  class="link-del" title="删除" href="javascript:;" onclick="javascript:deleteGoodsType(' + val.typeId + ',' + val.isParent + ')" >'
		 						+ '			删除'
		 						+ '		</a>'
		 						+ '	</td>'
		 						+ '</tr>';	
		 					if(val.isParent == 1 && val.subList) {
		 						treeTableData += recursionSubGoodsType(val.subList, val.typeId);
		 					}
						}
 					});
 					$("#treeTableBody").empty();
 					$("#treeTableBody").append(treeTableData);
 					$("#treeTable").treetable(option, true);
 				}
 			}
 		});
	}
	
	/**
	 * 递归添加分类子集
	 * @param resultList 结果List
	 * @param goodsTypePic 父级分类Id
	 */
	function recursionSubGoodsType(subList, typePid) {
		var result = '';
		$.each(subList, function (index, val) {
			var  buttonStr='';
			if(val.typeLevel<3){
				buttonStr+=
					 ' <td style="text-align: center;">'
					+ '		<a  class="link-add" title="添加" href="javascript:;" onclick="javascript:addSubGoodsType(' + val.typeId + ');">'
					+ '			添加'
					+ '		</a>'
					+ '		<a   class="link-update" title="修改" href="javascript:;" onclick="javascript:editGoodsType(' + val.typeId + ');">'
					+ '			修改'
					+ '		</a>'
					+ '		<a  class="link-del" title="删除" href="javascript:;" onclick="javascript:deleteGoodsType(' + val.typeId + ',' + val.isParent + ')" >'
					+ '			删除'
					+ '		</a>'
					+ '	</td>';
			}else{
				buttonStr+=
					' <td style="text-align: center;">'
					+ '	</td>';
			}
			result += 
                 '<tr data-tt-id="' + val.typeId + '" data-tt-parent-id="' + typePid + '">'
                + ' <td style="text-align: center;"><input type="checkbox" value="' + val.typeId + '" name="typeIdCheckBox" onclick="javascript:dynamicCheck(this);" /></td>'
                + '	<td>' + shiro.util.formatTableData("-",val.typeLevel) + '</td>'
				+ ' <td>' + shiro.util.formatTableData("-",val.typeName)+ '</td>'
				+ ' <td>' + shiro.util.formatTableData("-",val.typeDesc) + '</td>'
				+ buttonStr
				+ '</tr>';	
			if(val.isParent == 1 && val.subList) {
				result += recursionSubGoodsType(val.subList, val.typeId);
			}
		});
		return result;
	}

	// 动态勾选分类信息
	function dynamicCheck(obj) {
		var isChecked = $(obj).prop('checked');
		var subArray = new Array();
		recursionSub(obj, subArray);
		for(var i = 0; i < subArray.length; i++) {
			var sid = subArray[i];
			$('#treeTableBody').find("tr").each(function(){
				var typeId = $(this).attr('data-tt-id');
				if(sid == typeId) {
					var tdArr = $(this).children();
					tdArr.eq(0).find('input').prop('checked', isChecked);
				}
			});
		} 
	}
	
	// 递归查找子分类
	function recursionSub(obj, subArray) {
		var typeId = $(obj).parent().parent().attr('data-tt-id');
		$('#treeTableBody').find("tr").each(function(){
			var subGoodsTypeId = $(this).attr('data-tt-id');
			var subGoodsTypePid = $(this).attr('data-tt-parent-id');
			if(subGoodsTypePid == typeId) {
				subArray.push(subGoodsTypeId);
				var tdArr = $(this).children();
				recursionSub(tdArr.eq(0).find('input'), subArray);
			}
		});
	}
	
	
	function  addSubGoodsType(typeId){
		window.location.href=shiro.baseUrl + "/goodsType/addSubGoodsType?TypePid="+typeId;
	}
	
	function  editGoodsType(typeId){
		window.location.href=shiro.baseUrl + "/goodsType/editGoodsType?typeId="+typeId;
	}
	

// 添加分类
function executeGoodsTypeAdd() {
    // 表单验证
    var typeName = $.trim($('#typeName').val());
    if(shiro.util.isEmpty(typeName)) {
        layer.tips('分类名称不能为空', '#typeName');
        return false;
    }
    // 提交添加
    shiro.ajax({
        url: '/goodsType/executeAddGoodsType',
        data:$('#goodsTypeAddForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "goodsType/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#typeName').val('');
            }
        }
    });
    return false;
}

//添加根分类
function executeGoodsTypeSubAdd() {
	 // 表单验证
    var typeName = $.trim($('#typeName').val());
    if(shiro.util.isEmpty(typeName)) {
        layer.tips('分类名称不能为空', '#typeName');
        return false;
    }
    // 提交添加
    shiro.ajax({
        url: '/goodsType/executeAddSubGoodsType',
        data:$('#goodsTypeAddSubForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "goodsType/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#goodsTypeName').val('');
            }
        }
    });
    return false;
}

// 修改分类
function executeGoodsTypeEdit() {
	// 表单验证
    var typeName = $.trim($('#typeName').val());
    if(shiro.util.isEmpty(typeName)) {
        layer.tips('分类名称不能为空', '#typeName');
        return false;
    }
    // 提交修改
    shiro.ajax({
        url: 'goodsType/executeEditGoodsType',
        data:$('#goodsTypeEditForm').serialize(),
        success: function(data) {
            if(data.result == 0) {
                layer.msg(data.message, {icon: 1,time: 1000}, function() {
                    window.location.href = shiro.baseUrl + "goodsType/list";
                });
            } else {
                layer.msg(data.message, {icon: 5,time: 1000});
                $('#typeName').val('');
            }
        }
    });
    return false;
}

//删除分类信息
function deleteGoodsType(typeId, isParent) {
	var hitMessage = '确认要删除该分类？';
	if(isParent == 1) {
		hitMessage = '【系统提示】您想要删除的分类含有子分类，系统会同步删除所有子分类，请谨慎操作，确认删除？';
	}
	layer.confirm(hitMessage, {title:'系统消息'},function(index){
		shiro.ajax({
 			url: 'goodsType/delete',
 			data:{'typeId' : typeId},
 			success: function(data) {
 				if(data.result == 0) {
 					layer.msg(data.message, {icon: 1,time: 1500}, function() { 
 						window.location.href = shiro.baseUrl + "/goodsType/list";
 					});
 				} else {
 					layer.msg(data.message, {icon: 5,time: 1500});
 				}
 			}
 		});
	});
}

// 批量删除分类
function batchDeleteGoodsType() {
    var typeIdArray = shiro.form.getCheckBoxSelectValues('typeIdCheckBox');
    if(typeIdArray.length == 0) {
        layer.msg('请选择要操作的数据行', {icon: 5,time: 1000});
    } else {
        layer.msg('确定删除选中的分类？', {
            time: 0,
            btn: ['确定', '取消'],
            yes: function(index){
                layer.close(index);
                var typeIds = typeIdArray.join(',');
                // 提交删除
                shiro.ajax({
                    url: 'goodsType/batchDelete',
                    data:{"typeIds": typeIds},
                    success: function(data) {
                        if(data.result == 0) {
                            layer.msg(data.message, {icon: 1,time: 1000}, function() {
                                window.location.href = shiro.baseUrl + "goodsType/list";
                            });
                        } else {
                            layer.msg(data.message, {icon: 5,time: 1000});
                        }
                    }
                });
            }
        });
    }
}